package com.cpkf.util.iTextPDF;

import java.util.Iterator;

/**
 * text template analyze util, the template like this
 * === section ===
 * ==== subsection ====
 * list:
 * * A list
 * * Of bulleted items
 * * And back to the main bulleted list
 * anchor
 * [WikiSyntax#Wiki-style_markup]
 * table
 * || *Year* || *Temperature (low)* || *Temperature (high)* ||
 * || 1900 || -10 || 25 ||
 * || 1910 || -15 || 30 ||
 * || 1920 || -10 || 32 ||
 *
 * @author Hyman Hu
 * @since  2014-6-23
 * Copyright SHOP.COM: all rights reserved worldwide
 */
public class TextAnalyzeUtil {
	
	public static final String TITLE_SYMBOL = "=";
	public static final String TITLE_SYMBOL_START = "= ";
	public static final String TITLE_SYMBOL_END = " =";
	public static final String CHAPTER_SYMBOL = "==";
	public static final String CHAPTER_SYMBOL_START = "== ";
	public static final String CHAPTER_SYMBOL_END = " ==";
	public static final String SECTION_SYMBOL = "===";
	public static final String SECTION_SYMBOL_START = "=== ";
	public static final String SECTION_SYMBOL_END = " ===";
	public static final String SUBSECTION_SYMBOL = "====";
	public static final String SUBSECTION_SYMBOL_START = "==== ";
	public static final String SUBSECTION_SYMBOL_END = " ====";
	public static final String LIST_SYMBOL = "*";
	public static final String TABLE_SYMBOL = "||";
	public static final String ANCHOR_SYMBOL_START = "[";
	public static final String ANCHOR_SYMBOL_END = "]";
	public static final String ANCHOR_SEPARATOR_SYMBOL = "#";
		
	/**
	 * judge sting category
	 * 
	 * @param line		line string
	 * @return ElementCategory
	 */
	public static ElementCategory judgeCategory(String line) {
		line = line.trim();
		Iterator<Integer> iterator = ElementCategory.categoryMap.keySet().iterator();
		ElementCategory elementCategoriy = ElementCategory.getElementCategory(0);
		boolean isContent = true;
		while(iterator.hasNext()) {
			elementCategoriy = ElementCategory.getElementCategory(iterator.next());
			Integer temp = line.indexOf(elementCategoriy.symbol);
			if (!elementCategoriy.symbol.isEmpty()) {
				if (temp == 0 && !(elementCategoriy.symbol.charAt(0) + "").equals(line.charAt(
						elementCategoriy.symbol.length() - 1))) {
					isContent = false;
					break;
				}
			}
		}
		if (isContent) {
			elementCategoriy = ElementCategory.getElementCategory(7);
		}
		
		return elementCategoriy;
	}
	
	/**
	 * get title string
	 * 
	 * @param line		line string
	 * @return String
	 */
	public static String getTitle(String line) {
		String title = "";
		if (line.isEmpty()) {
			return title;
		}
		
		line = line.trim();
		int start = line.indexOf(TITLE_SYMBOL_START);
		int end = line.indexOf(TITLE_SYMBOL_END);
		if (start != -1 && end != -1) {
			title = line.substring(start + TITLE_SYMBOL_START.length(), end);
		}
		
		return title;
	}
	
	/**
	 * get chapter string
	 * 
	 * @param line		line string
	 * @return String
	 */
	public static String getChapter(String line) {
		String chapter = "";
		if (line.isEmpty()) {
			return chapter;
		}
		
		line = line.trim();
		int start = line.indexOf(CHAPTER_SYMBOL_START);
		int end = line.indexOf(CHAPTER_SYMBOL_END);
		if (start != -1 && end != -1) {
			chapter = line.substring(start + CHAPTER_SYMBOL_START.length(), end);
		}
		
		return chapter;
	}
	
	/**
	 * get section string
	 * 
	 * @param line		line string
	 * @return string
	 */
	public static String getSection(String line) {
		String section = "";
		if (line.isEmpty()) {
			return section;
		}
		
		line = line.trim();
		int start = line.indexOf(SECTION_SYMBOL_START);
		int end = line.indexOf(SECTION_SYMBOL_END);
		if (start != -1 && end != -1) {
			section = line.substring(start + SECTION_SYMBOL_START.length(), end);
		}
		
		return section;
	}
	
	
	/**
	 * get subsection string
	 * 
	 * @param line		line string
	 * @return string
	 */
	public static String getSubsection(String line) {
		String subchapter = "";
		if (line.isEmpty()) {
			return subchapter;
		}
		
		line = line.trim();
		int start = line.indexOf(SUBSECTION_SYMBOL_START);
		int end = line.indexOf(SUBSECTION_SYMBOL_END);
		if (start != -1 && end != -1) {
			subchapter = line.substring(start + SUBSECTION_SYMBOL_START.length(), end);
		}
		
		return subchapter;
	}
	
	/**
	 * get anchor string
	 * 
	 * @param line		line string
	 * @return string
	 */
	public static String getAnchor(String line) {
		String anchor = "";
		if (line.isEmpty()) {
			return anchor;
		}
		
		line = line.trim();
		int start = line.indexOf(ANCHOR_SYMBOL_START);
		int end = line.indexOf(ANCHOR_SYMBOL_END);
		if (start != -1 && end != -1) {
			anchor = line.substring(start + ANCHOR_SYMBOL_START.length(), end);
		}
		
		return anchor;
	}
	
	/**
	 * get each list child string
	 * 
	 * @param line		line string
	 * @return string
	 */
	public static String getListChild(String line) {
		String listChild = "";
		if (line.isEmpty()) {
			return listChild;
		}
		
		line = line.trim();
		int start = line.indexOf(LIST_SYMBOL);
		if (start != -1) {
			listChild = line.substring(start + ANCHOR_SYMBOL_START.length(), line.length());
		}
		
		return listChild;
	}
}
